﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using HotelManagement._04.OBJECT;

namespace HotelManagement._03.DAO
{
    class CustomerManagerDAO
    {
        DbConnection m_conn;
        SqlCommand m_command;
        SqlDataAdapter m_adap;
        public CustomerManagerDAO()
        {
            m_conn = new DbConnection();
        }
        public DataTable ViewAll(string ip_procedure)
        {
            m_command = new SqlCommand(ip_procedure, m_conn.Connect());
            m_command.CommandType = CommandType.StoredProcedure;
            DataTable v_dt = new DataTable();
            m_adap = new SqlDataAdapter(m_command);
            try
            {
                m_adap.Fill(v_dt);
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return v_dt;
        }
        public DataTable ViewAllCustomerType(string ip_procedure)
        {
            m_command = new SqlCommand(ip_procedure, m_conn.Connect());
            m_command.CommandType = CommandType.StoredProcedure;
            DataTable v_dt = new DataTable();
            m_adap = new SqlDataAdapter(m_command);
            try
            {
                m_adap.Fill(v_dt);
                m_conn.Close();
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return v_dt;
        }
        public bool Update(Customer customer,string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@CustomerID ", SqlDbType.BigInt).Value = customer.CustomerID;
                m_command.Parameters.Add("@CustomerName ", SqlDbType.NVarChar).Value = customer.CustomerName;
                m_command.Parameters.Add("@Birthday ", SqlDbType.DateTime).Value = customer.Birthday;
                m_command.Parameters.Add("@Gender ", SqlDbType.NChar).Value = customer.Gender;
                m_command.Parameters.Add("@Nationality ", SqlDbType.NVarChar).Value = customer.Nationality;
                m_command.Parameters.Add("@Address ", SqlDbType.NVarChar).Value = customer.Address;
                m_command.Parameters.Add("@Phone ", SqlDbType.VarChar).Value = customer.Phone;
                m_command.Parameters.Add("@Email ", SqlDbType.NVarChar).Value = customer.Email;
                m_command.Parameters.Add("@IDCardNo  ", SqlDbType.VarChar).Value = customer.IDCardNo;
                m_command.Parameters.Add("@PassportNo ", SqlDbType.VarChar).Value = customer.PassportNo;
                m_command.Parameters.Add("@CustomerTypeID ", SqlDbType.BigInt).Value = customer.CustomerTypeID;
                m_command.Parameters.Add("@Note ", SqlDbType.NVarChar).Value = customer.Note;
                m_adap = new SqlDataAdapter(m_command);
                m_command.ExecuteNonQuery();
                m_conn.Close();
                return true;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return false;
        }

        public DataTable Insert (Customer customer, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                //m_command.Parameters.Add("@CustomerID ", SqlDbType.BigInt).Value = customer.CustomerID;
                m_command.Parameters.Add("@CustomerName ", SqlDbType.NVarChar).Value = customer.CustomerName;
                m_command.Parameters.Add("@Birthday ", SqlDbType.DateTime).Value = customer.Birthday;
                m_command.Parameters.Add("@Gender ", SqlDbType.NChar).Value = customer.Gender;
                m_command.Parameters.Add("@Nationality ", SqlDbType.NVarChar).Value = customer.Nationality;
                m_command.Parameters.Add("@Address ", SqlDbType.NVarChar).Value = customer.Address;
                m_command.Parameters.Add("@Phone ", SqlDbType.VarChar).Value = customer.Phone;
                m_command.Parameters.Add("@Email ", SqlDbType.NVarChar).Value = customer.Email;
                m_command.Parameters.Add("@IDCardNo  ", SqlDbType.VarChar).Value = customer.IDCardNo;
                m_command.Parameters.Add("@PassportNo ", SqlDbType.VarChar).Value = customer.PassportNo;
                m_command.Parameters.Add("@CustomerTypeID ", SqlDbType.BigInt).Value = customer.CustomerTypeID;
                m_command.Parameters.Add("@Note ", SqlDbType.NVarChar).Value = customer.Note;
                m_adap = new SqlDataAdapter(m_command);

                DataTable v_dt = new DataTable();
                m_adap.Fill(v_dt);
                m_conn.Close();
                return v_dt;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return null;
        }
        public bool Delete(Customer customer, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@CustomerID ", SqlDbType.BigInt).Value = customer.CustomerID;
                m_adap = new SqlDataAdapter(m_command);
                m_command.ExecuteNonQuery();
                m_conn.Close();
                return true;
            }
            catch (Exception ex)
            {

                MessageBox.Show("Không thể xóa thông tin những khách hàng đã từng sử dụng dịch vụ tại khách sạn ! ");
            }
            return false;
        }
        public bool Find(Customer customer, string ip_procedure)
        {
            try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@passport ", SqlDbType.VarChar).Value = customer.PassportNo;
                m_adap = new SqlDataAdapter(m_command);
                DataTable v_dt = new DataTable();
                m_adap.Fill(v_dt);
                m_conn.Close();
                if (v_dt.Rows.Count >0)
                    return true;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return false;
        }

        public DataTable Search(Customer customer, String ip_procedure)
        {
          try
            {
                m_command = new SqlCommand(ip_procedure, m_conn.Connect());
                m_command.CommandType = CommandType.StoredProcedure;
                m_command.Parameters.Add("@CustomerName ", SqlDbType.NVarChar).Value = customer.CustomerName;
               // m_command.Parameters.Add("@Birthday ", SqlDbType.NVarChar).Value = customer.Birthday.ToString();
                //m_command.Parameters.Add("@Gender ", SqlDbType.NVarChar).Value = customer.Gender;
                //m_command.Parameters.Add("@Nationality ", SqlDbType.NVarChar).Value = customer.Nationality;
                //m_command.Parameters.Add("@Address ", SqlDbType.NVarChar).Value = customer.Address;
                //m_command.Parameters.Add("@Phone ", SqlDbType.NVarChar).Value = customer.Phone;
                //m_command.Parameters.Add("@Email ", SqlDbType.NVarChar).Value = customer.Email;
                //m_command.Parameters.Add("@IDCardNo  ", SqlDbType.NVarChar).Value = customer.IDCardNo;
                //m_command.Parameters.Add("@PassportNo ", SqlDbType.NVarChar).Value = customer.PassportNo;
                //m_command.Parameters.Add("@CustomerTypeID ", SqlDbType.BigInt).Value = customer.CustomerTypeID;
                m_adap = new SqlDataAdapter(m_command);

                DataTable v_dt = new DataTable();
                m_adap.Fill(v_dt);
                m_conn.Close();
                return v_dt;
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            return null;
        }
        }
    }
